package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"log/slog"
	"net/http"
	"os"
	"strings"
)

var (
	URLicenseServer string
	license         string
)

func main() {
	fmt.Println("Hello World kso fmt ")
	//fmtzhanwei()
	//jisuan()
	//test31()
	//fmttest99()
	//RegexpCase()
	//fmtstringMain()
	//zhengzemain()
	//testfmt()
	//./agentctl install --license idhMRQY5t
	//ansd := os.Args
	//ass := os.Args[1:]
	//arg := os.Args[3]
	//fmt.Println("asddddddkso", ansd)
	//fmt.Println("asddddddkso", ass)
	//fmt.Println("asddddddkso", arg)
	//var license = flag.String("license", "", "license")
	//var install = flag.Bool("install", false, "install")
	//flag.Parse()
	//fmt.Println("Using License Key: ", *license)
	//fmt.Println("Installation flag is not set : ", *install)

	//installCmd := flag.NewFlagSet("install", flag.ExitOnError)
	//installCmd.StringVar(&license, "license", "", "The Volume License")
	//
	//uninstallCmd := flag.NewFlagSet("uninstall", flag.ExitOnError)
	//fmt.Println("kso uninstallCmd:", *uninstallCmd)
	//fmt.Println("kso installCmd:", *installCmd)
	//fmt.Println("kso license:", license)
	//var license string
	//
	//// 定义子命令及其标志
	//checkEnvCmd := flag.NewFlagSet("checkenv", flag.ExitOnError)
	//checkEnvCmd.Usage = printCheckEnvUsage
	//
	//installCmd := flag.NewFlagSet("install", flag.ExitOnError)
	//installCmd.StringVar(&license, "license", "", "The Volume License")
	//installCmd.Usage = printInstallUsage
	//
	//uninstallCmd := flag.NewFlagSet("uninstall", flag.ExitOnError)
	//uninstallCmd.Usage = printUninstallUsage
	//
	//// 检查命令及解析相应的子命令标志
	//if len(os.Args) < 2 {
	//	fmt.Println("expected 'checkenv', 'install', or 'uninstall' subcommands")
	//	printUsage()
	//	os.Exit(1)
	//}
	//
	//switch os.Args[1] {
	//case "checkenv":
	//	checkEnvCmd.Parse(os.Args[2:])
	//	fmt.Println("CheckEnv command executed.")
	//case "install":
	//	installCmd.Parse(os.Args[2:])
	//	fmt.Println("Install command executed with license:", license)
	//case "uninstall":
	//	uninstallCmd.Parse(os.Args[2:])
	//	fmt.Println("Uninstall command executed.")
	//default:
	//	fmt.Println("expected 'checkenv', 'install', or 'uninstall' subcommands")
	//	printUsage()
	//	os.Exit(1)
	//}

	//foocmd := flag.NewFlagSet("foo", flag.ExitOnError)
	//fooenable := foocmd.Bool("enable", false, "Enable the kso")
	//fooName := foocmd.String("name", "", "Name of the kso")
	//
	//barcmd := flag.NewFlagSet("bar", flag.ExitOnError)
	//barenable := barcmd.Int("enable", 4, "Enable the kso")
	//
	//if len(os.Args) < 2 {
	//	fmt.Println("Usage: go run main.go foo -enable=true")
	//	os.Exit(1)
	//}
	//switch os.Args[1] {
	//case "foo":
	//	foocmd.Parse(os.Args[2:])
	//	fmt.Println("foo enable :", *fooenable)
	//	fmt.Println("foo fooName :", *fooName)
	//
	//case "bar":
	//	barcmd.Parse(os.Args[2:])
	//	fmt.Println("bar enable :", *barenable)
	//	fmt.Println("  tail:", barcmd.Args())
	//default:
	//	fmt.Println("Usage: go run main.go foo -enable=true")
	//	os.Exit(1)
	//}
	log.Printf("standard logger ")
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	log.Println(" with micro .... ")
	log.Println("standard logger ....")
	log.Println("with micro .... ")
	log.New(os.Stdout, "my:", log.LstdFlags|log.Lshortfile)
	mylog := log.New(os.Stdout, "my:", log.LstdFlags|log.Lshortfile)
	mylog.SetFlags(log.LstdFlags | log.Lshortfile)
	mylog.Println("from my log ....")
	mylog.SetPrefix("ohmy: ....")

	var buf bytes.Buffer
	buflog := log.New(&buf, "buf:", log.LstdFlags|log.Lshortfile)
	buflog.Println("from my log ....")
	var js = slog.NewJSONHandler(os.Stderr, nil)
	mylogs := slog.New(js)
	mylogs.Info("hi there . .. . .  .. . . ..  .")
	url := "http://10.12.23.40:45124/api/nodeQuery/getActiveNode?size=500"

	// 创建一个新的 HTTP 请求
	req, err := http.NewRequest("POST", url, nil)
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}

	// 设置请求头
	req.Header.Set("Accesskey", `{"appkey":"1df283876464491397fdc461a37f4c24","timestamp":"1704772588583","requestId":"f075b85cdc3a4ae291b6bfc2a087266f","signature":"ce0e02127dbe6fbcebd8842f0821f1e6"}`)
	// 创建一个 HTTP 客户端并发送请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error making request:", err)
		return
	}
	defer resp.Body.Close()

	// 读取和输出响应
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("Error reading response:", err)
		return
	}

	fmt.Println("Response status:", resp.Status)
	fmt.Println("Response body:", string(body))
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/headers", headers)

	http.ListenAndServe(":8090", nil)
}
func hello(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(w, "hellasdasdasdo\n")
}

func headers(w http.ResponseWriter, req *http.Request) {

	for name, headers := range req.Header {
		for _, h := range headers {
			fmt.Fprintf(w, "%v: %v\n", name, h)
		}
	}
}

func variablesd() {
	os.Setenv("URLICENSE_SERVER", URLicenseServer)
	fmt.Println("URLICENSE_SERVER:", os.Getenv("URLICENSE_SERVER"))
	fmt.Println("BAR:", os.Getenv("BAR"))
	fmt.Println()
	for _, e := range os.Environ() {
		pair := strings.SplitN(e, "=", 2)
		fmt.Println(pair[0])
	}
}

func printCheckEnvUsage() {
	fmt.Println("checkenv will check if the hardware and software meet the requirement")
}
func printInstallUsage() {
	fmt.Println(`install install --license license_string`)
}
func printUninstallUsage() {
	fmt.Println("uninstall will uninstall all the components. Use TUI")
}
func printUsage() {
	fmt.Println(`Usage:
    version
    checkenv
    checklicense
    install
    uninstall
    upgrade
    imageOperation
	clean`)
}
